package net.esj.basic.core.view.tools;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.esj.basic.core.engine.tools.crypt.Crypt;
import net.esj.basic.core.view.tools.searcher.Searcher;
import net.esj.basic.core.view.tools.searcher.SearcherColumn;
import net.esj.basic.core.view.tools.searcher.SearcherImpl;

import org.apache.struts2.ServletActionContext;

public class AgentContextStrutsImpl implements AgentContext {
	
	private long cookieMaxAge = 1000 * 60 * 60 * 24 * 14;//默认2周
	
	private Crypt crypt;

	@Override
	public ClientAgent getClient() {
		return new ClientAgent((HttpServletRequest) getRequest());
	}

	@Override
	public ServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	@Override
	public ServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	@Override
	public Searcher getSearcher(String key){
		Map<String,Searcher> searchers = (Map<String, Searcher>) getSession().getAttribute(Searcher.REQUEST_SEARCH_KEY);
		if(searchers==null){
			searchers = new HashMap<String, Searcher>();
			getSession().setAttribute(Searcher.REQUEST_SEARCH_KEY, searchers);
		}
		Searcher searcher =searchers.get(key);
		if(searcher==null){
			searcher = new SearcherImpl(key);
			searchers.put(key, searcher);
		}
		return searcher;
	}
	
	@Override
	public void clearAllSearchers() {
		Map<String,Searcher> searchers = (Map<String, Searcher>) getSession().getAttribute(Searcher.REQUEST_SEARCH_KEY);
		if(searchers==null){
			return;
		}
		searchers.clear();
	}

	@Override
	public void clearSearcher(String key) {
		Map<String,Searcher> searchers = (Map<String, Searcher>) getSession().getAttribute(Searcher.REQUEST_SEARCH_KEY);
		if(searchers==null){
			return;
		}
		searchers.remove(key);
	}
	
	@Override
	public void addSearchColumn(String searcherKey, String key, String type,String view,
			Object value) {
		Searcher searcher = getSearcher(searcherKey);
		searcher.newColumn(key, view,type, value);
	}
	
	@Override
	public void addSearchColumn(String searcherKey, SearcherColumn column) {
		Searcher searcher = getSearcher(searcherKey);
		searcher.newColumn(column);
	}
	
	@Override
	public void addSearchOrder(String searcherKey, String key, String view,
			Object value, int order) {
		Searcher searcher = getSearcher(searcherKey);
		searcher.newOrderBy(key, view, value,order);
	}

	@Override
	public void addSearchOrder(String searcherKey, SearcherColumn column,
			int order) {
		Searcher searcher = getSearcher(searcherKey);
		searcher.newOrderBy(column,order);
	}

	@Override
	public void addSearchOrder(String searcherKey, String key, String view,
			Object value) {
		Searcher searcher = getSearcher(searcherKey);
		searcher.newOrderBy(key, view, value);
	}

	@Override
	public void addSearchOrder(String searcherKey, SearcherColumn column) {
		Searcher searcher = getSearcher(searcherKey);
		searcher.newOrderBy(column);
	}

	@Override
	public void clearSearchOrder(String searcherKey) {
		Searcher searcher = getSearcher(searcherKey);
		searcher.clearOrderAll();
	}


	@Override
	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	@Override
	public void addRequestAttribute(String key, Object o) {
		getRequest().setAttribute(key, o);
	}

	@Override
	public void addSessionAttribute(String key, Object o) {
		getSession().setAttribute(key, o);
	}

	@Override
	public void addCookie(Cookie cookie) {
		cookie.setMaxAge((int) cookieMaxAge);
		((HttpServletResponse)getResponse()).addCookie(cookie);
	}

	@Override
	public void addCookie(String key, String value,boolean needEncrypt) {
		if(needEncrypt && crypt!=null){
			value = crypt.encrypt(value);
		}
		Cookie cookie  = new Cookie(key, value);
		cookie.setMaxAge((int) cookieMaxAge);
		((HttpServletResponse)getResponse()).addCookie(cookie);
	}

	@Override
	public String getCookieValue(String key, boolean needDecrypt) {
		Cookie[] cookies = ((HttpServletRequest)getRequest()).getCookies();
		if(cookies!=null){
			for (int i = 0; i < cookies.length; i++) {
				Cookie c = cookies[i];  
				if(c.getName().equalsIgnoreCase(key)){
					if(needDecrypt && crypt!=null){
						return crypt.decrypt(c.getValue());
					}
				}
			}
		}
		return null;
	}

	@Override
	public long getCookieMaxAge() {
		return cookieMaxAge;
	}

	public void setCookieMaxAge(long cookieMaxAge) {
		this.cookieMaxAge = cookieMaxAge;
	}

	public void setCrypt(Crypt crypt) {
		this.crypt = crypt;
	}

}
